package com.wlcj.www.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import jodd.datetime.JDateTime;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.IocBean;

import com.jfinal.plugin.activerecord.Db;
import com.wlcj.util.RedisUtil;
import com.wlcj.www.model.data.Data;
import com.wlcj.www.model.platform.Platform;
import com.wlcj.www.model.data.PlatformAttention;
import com.wlcj.www.model.data.Province;
import com.wlcj.www.util.DataUtil;
import com.wlcj.www.util.Lang;

@IocBean
public class AttentionService {
	static List<String> commonProvinceList = Arrays.asList(new String[]{"19", "2", "10", "12", "11", "3", "23", "17"});
	public List<PlatformAttention> getDataList(String time, String orderby, String provinceId,String userId) {
		List<PlatformAttention> dataList = PlatformAttention.dao.getDataListByorder(time, orderby, userId);
		List<PlatformAttention> copyList = new ArrayList<PlatformAttention>();
		for(PlatformAttention d:dataList) {
			Integer platformId = d.getInt("platform_id");
			Platform pf = Platform.dao.getCachePlatformById(platformId);
			d.put("platform_name", pf.getStr("platform_name"));
			BigDecimal volume = d.getBigDecimal("volume");
			d.put("volume_display", Lang.formatMoney(volume));
			d.put("compound_interest_display", Lang.formatMoney(d.getDouble("compound_interest")));
			d.put("average_loan_time_display", Lang.formatMoney(d.getDouble("average_loan_time")));
			BigDecimal avInvestAmount = d.getBigDecimal("av_investment_amount");
			d.put("av_investment_amount_display", Lang.formatMoney(avInvestAmount));
			BigDecimal avBorroAmount = d.getBigDecimal("av_borrow_amount");
			d.put("av_borrow_amount_display", Lang.formatMoney(avBorroAmount));
			d.put("collecting_10_display", Lang.formatMoney(d.getDouble("10_collecting")));
			d.put("borrowers_10_display", Lang.formatMoney(d.getDouble("10_borrowers")));
			BigDecimal timeWeightVolume = d.getBigDecimal("time_weighted_volume");
			d.put("time_weighted_volume_display", Lang.formatMoney(timeWeightVolume));
			d.put("full_biaoyong_time_display", DataUtil.formatSecondStr(d.getDouble("full_biaoyong_time")));
			d.put("operating_time_display", DataUtil.formatMonthStr(d.getInt("operating_time")));
			BigDecimal registerCapital = d.getBigDecimal("registered_capital");
			d.put("registered_capital_display", Lang.formatMoney(registerCapital));
			d.put("leverage_display", Lang.formatMoney(d.getDouble("leverage")));
			BigDecimal inflow30 = d.getBigDecimal("30_inflows");
			d.put("inflows_30_display", Lang.formatMoney(inflow30));
			BigDecimal still60 = d.getBigDecimal("60_still");
			d.put("still_60_display", Lang.formatMoney(still60));
			BigDecimal cumulativeAmount = d.getBigDecimal("cumulative_amount");
			d.put("cumulative_amount_display", Lang.formatMoney(cumulativeAmount));
			
			if(StringUtils.isNotBlank(provinceId)) {
				String pid = pf.getInt("province_id")+"";
				boolean flag1 = provinceId.equals("other")&&!commonProvinceList.contains(pid);
				boolean flag2 = provinceId.equals(pid);
				if(flag1||flag2)
					copyList.add(d);
			}
		}
		if(StringUtils.isBlank(provinceId))
			return dataList;
		else
			return copyList;
	}
	
public List<PlatformAttention> getDataList(String[] time, String orderby, String provinceId,String userId){
		List<PlatformAttention> dataList = PlatformAttention.dao.getDataList(time[0],time[1],orderby,userId);
		List<PlatformAttention> copyList = new ArrayList<PlatformAttention>();
		for(PlatformAttention d:dataList) {
			Integer platformId = d.getInt("platform_id");
			Platform pf = Platform.dao.getCachePlatformById(platformId);
			d.put("platform_name", pf.getStr("platform_name"));
			BigDecimal volume = d.getBigDecimal("volume");
			d.put("volume_display", Lang.formatMoney(volume));
			d.put("compound_interest_display", Lang.formatMoney(d.getDouble("compound_interest")));
			d.put("average_loan_time_display", Lang.formatMoney(d.getDouble("average_loan_time")));
			BigDecimal avInvestAmount = d.getBigDecimal("av_investment_amount");
			if(avInvestAmount==null){
				d.put("av_investment_amount_display", new BigDecimal("0"));
			}else{
				d.put("av_investment_amount_display", Lang.formatMoney(avInvestAmount));
			}
			BigDecimal avBorroAmount = d.getBigDecimal("av_borrow_amount");
			if(avBorroAmount==null){
				d.put("av_borrow_amount_display", new BigDecimal("0"));
			}else{
				d.put("av_borrow_amount_display", Lang.formatMoney(avBorroAmount));
			}
			d.put("collecting_10_display", Lang.formatMoney(d.getDouble("10_collecting")));
			d.put("borrowers_10_display", Lang.formatMoney(d.getDouble("10_borrowers")));
			Double timeWeightVolume = d.getDouble("time_weighted_volume");
			d.put("time_weighted_volume_display", Lang.formatMoney(timeWeightVolume));
			d.put("full_biaoyong_time_display", DataUtil.formatSecondStr(d.getDouble("full_biaoyong_time")));
			d.put("operating_time_display", DataUtil.formatMonthStr(d.getInt("operating_time")));
			BigDecimal registerCapital = d.getBigDecimal("registered_capital");
			d.put("registered_capital_display", Lang.formatMoney(registerCapital));
			d.put("leverage_display", Lang.formatMoney(d.getDouble("leverage")));
			BigDecimal inflow30 = d.getBigDecimal("30_inflows");
			d.put("inflows_30_display", Lang.formatMoney(inflow30));
			BigDecimal still60 = d.getBigDecimal("60_still");
			d.put("still_60_display", Lang.formatMoney(still60));
			BigDecimal cumulativeAmount = d.getBigDecimal("cumulative_amount");
			d.put("cumulative_amount_display", Lang.formatMoney(cumulativeAmount));
			
			if(StringUtils.isNotBlank(provinceId)) {
				String pid = pf.getInt("province_id")+"";
				boolean flag1 = provinceId.equals("other")&&!commonProvinceList.contains(pid);
				boolean flag2 = provinceId.equals(pid);
				if(flag1||flag2)
					copyList.add(d);
			}
		}
		if(StringUtils.isBlank(provinceId))
			return dataList;
		else
			return copyList;
	}
	

	
	@Aop("tx")
	public void dellUserAttentionPlatform(int uid, int pid) {
		String sql = "delete from wd_new_platform_attention where user_id = ? and attention_id = ?";
		Db.update(sql, uid, pid);
	}
	
	public List<Province> getAllProvinces() {
		String sql = "select provinces_id from wd_d_provinces";
		List<Province> list = Province.dao.find(sql);
		for(Province p:list) {
			p.setAttrs(Province.dao.getCacheProvinceById(p.getInt("provinces_id")));
		}
		return list;
	}

	public List<Data> getAttentionDataList(int userId,String time, String sortField,Integer[] platformIdArray, Integer dayOrMonth){
		platformIdArray = DataUtil.removeZero(platformIdArray);
		
		if(platformIdArray==null){
			List<Integer> ids = PlatformAttention.dao.getUserAttentionPlatformIds(userId,time);//成交量倒序
			List<Data> data = new ArrayList<Data>();
			if(ids!=null&&ids.size()>4){
				for(int i=0;i<4;i++){
					String key = ids.get(i)+"-"+"day";
					//System.out.println(RedisUtil.get(key));
					List<Data> d =RedisUtil.get(key);
					data.addAll(d);
				}
			}
			return data;
		}else{
			List<Data> data = new ArrayList<Data>();
			switch (dayOrMonth) {
			case 1:
				for(int i=0;i<platformIdArray.length;i++){
					List<Data> d = RedisUtil.get(platformIdArray[i]+"-"+"day");
					data.addAll(d);
				}
				break;
			case 2:
				for(int i=0;i<platformIdArray.length;i++){
					List<Data> d = new ArrayList<Data>();
					if ("volume".equals(sortField)|| "investment_num".equals(sortField)|| "borrower_num".equals(sortField)){
						 d = RedisUtil.get("sum"+"-"+platformIdArray[i]+"-"+"week");
					}else if ("30_inflows".equals(sortField)|| "60_still".equals(sortField)|| "cumulative_amount".equals(sortField)){
						 d = RedisUtil.get("common"+"-"+platformIdArray[i]+"-"+"week");
					}else{
						 d = RedisUtil.get("avg"+"-"+platformIdArray[i]+"-"+"week");
					}
					data.addAll(d);
				}
				break;
			case 3:
				for(int i=0;i<platformIdArray.length;i++){
					List<Data> d = new ArrayList<Data>();
					if ("volume".equals(sortField)|| "investment_num".equals(sortField)|| "borrower_num".equals(sortField)){
						 d = RedisUtil.get("sum"+"-"+platformIdArray[i]+"-"+"month");
					}else if ("30_inflows".equals(sortField)|| "60_still".equals(sortField)|| "cumulative_amount".equals(sortField)){
						 d = RedisUtil.get("common"+"-"+platformIdArray[i]+"-"+"month");
					}else{
						 d = RedisUtil.get("avg"+"-"+platformIdArray[i]+"-"+"month");
					}
					data.addAll(d);
				}
				break;
			default:
				return null;
			}
			return data;
		}
	}
	
	public List<PlatformAttention> platform(int userId,String time){
		List<PlatformAttention> plat = PlatformAttention.dao.getUserPlatform(userId,time);
		return plat;
	}
}
